package com.example.redistest2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2025-03-30
 * Time: 15:42
 */
@RestController
public class MyController {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/testString")
    public String testString() {
        //redisTemplage将不同类型的方法进行了重新排放
        redisTemplate.opsForValue().set("key1","111");
        redisTemplate.opsForValue().set("key2","222");
        redisTemplate.opsForValue().set("key2","333");

        String value = redisTemplate.opsForValue().get("key1");
        System.out.println("value: " + value);

        return "ok";
    }

    @GetMapping("/testList")
    public String testList() {
        //使用redis原来的命令
        redisTemplate.execute((RedisConnection connetion) -> {
            connetion.flushAll();
            return null;
        });

        redisTemplate.opsForList().leftPush("key","111");
        redisTemplate.opsForList().leftPush("key","222");
        redisTemplate.opsForList().leftPush("key","333");

        String value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value: " + value);

        value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value: " + value);

        value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value: " + value);

        return "ok";
    }


    @GetMapping("/testSet")
    public String testSet() {
        //使用redis原来的命令
        redisTemplate.execute((RedisConnection connetion) -> {
            connetion.flushAll();
            return null;
        });

        redisTemplate.opsForSet().add("key","111","222","333");

        Set<String> value = redisTemplate.opsForSet().members("key");
        System.out.println("value: " + value);

        Boolean exists = redisTemplate.opsForSet().isMember("key","111");
        System.out.println("exists: " + exists);

        long result = redisTemplate.opsForSet().size("key");
        System.out.println("count: " +result);

        redisTemplate.opsForSet().remove("key","111","222");

        value = redisTemplate.opsForSet().members("key");
        System.out.println("value: " + value);

        return "ok";
    }


    @GetMapping("/testHash")
    public String testHash() {
        //使用redis原来的命令
        redisTemplate.execute((RedisConnection connetion) -> {
            connetion.flushAll();
            return null;
        });

        redisTemplate.opsForHash().put("key","f1","111");
        redisTemplate.opsForHash().put("key","f2","222");
        redisTemplate.opsForHash().put("key","f3","333");

        String value = (String) redisTemplate.opsForHash().get("key","f1");
        System.out.println("value: " + value);

        Boolean exists = redisTemplate.opsForHash().hasKey("key","f1");
        System.out.println("exists: " + exists);

        redisTemplate.opsForHash().delete("key","f1","f2");

        long result = redisTemplate.opsForHash().size("key");
        System.out.println("count: " +result);

        return "ok";
    }

    @GetMapping("/testZSet")
    public String testZSet() {
        //使用redis原来的命令
        redisTemplate.execute((RedisConnection connetion) -> {
            connetion.flushAll();
            return null;
        });

        redisTemplate.opsForZSet().add("key","zhangsan",10);
        redisTemplate.opsForZSet().add("key","lisi",20);
        redisTemplate.opsForZSet().add("key","wangwu",30);


        Set<String> value = redisTemplate.opsForZSet().range("key",0,-1);
        System.out.println("value: " + value);

        Set<ZSetOperations.TypedTuple<String>>value1 = redisTemplate.opsForZSet().rangeWithScores("key",0,-1);
        System.out.println("value1: " + value1);

        Double result = redisTemplate.opsForZSet().score("key","zhangsan");
        System.out.println("count: " +result);

        redisTemplate.opsForZSet().remove("key","zhangsan");

        Long size = redisTemplate.opsForZSet().size("key");
        System.out.println("size: " + size);

        Long rank =  redisTemplate.opsForZSet().rank("key","lisi");
        System.out.println("rank: " + rank);
        return "ok";
    }
}
